package com.example.joysinfo.lkc;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.util.Log;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by joysinfo on 2017/3/22.
 */
public class FirstService extends Service {

    public final static String TAG = "xing";
    PowerManager.WakeLock wakeLock = null;

    @Override
    public void onCreate() {
        super.onCreate();

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FirstService");
        wakeLock.acquire();

        thread.start();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand");
        return START_STICKY;
    }

    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {

                @Override
                public void run() {
            Log.d("xing", "在跑吗"+MainActivity.isrun());
            if(!MainActivity.isrun()){
                MainActivity.torun();
            }

//                    boolean bc = Until.isServiceWorked(FirstService.this, "com.example.joysinfo.dangjian.SecondService");
//                    if(!bc) {
//
//                        Intent service = new Intent(FirstService.this, SecondService.class);
//                        startService(service);
//                    }
//                    boolean b = Until.isRun(FirstService.this);
//                    Log.e("xing", "在跑吗"+b);
//                    if(!b) {
//                        Log.e("xing", "1要跑");
//                        PackageManager manager = FirstService.this.getPackageManager();
//                        Intent openQQ = manager.getLaunchIntentForPackage("com.example.joysinfo.dangjian");
//                        FirstService.this.startActivity(openQQ);
//                    }
                }
            };
            timer.schedule(task, 0, 60000);
        }
    });

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onDestroy() {
        if (wakeLock != null) {
            wakeLock.release();
            wakeLock = null;
        }
        super.onDestroy();
    }
}
